package com.lvgang.springbootrabbitmq.routing;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;

/**
 * @author lvgang
 */
@Component
public class RoutingSender {

	private static Logger logger = LoggerFactory.getLogger(RoutingSender.class);
	@Autowired
	private RabbitTemplate rabbitTemplate;

	public void send(int i) {
		String contentA = "Routing A="+ i+"," + new Date() + ", content= " + UUID.randomUUID().toString();
		String contentB = "Routing B="+ i+"," + new Date() + ", content= " + UUID.randomUUID().toString();

		//消息发送，使用void convertAndSend(String exchange, String routingKey, Object message) throws AmqpException;
		//但不指定routingKey。因为FanoutExchange类型的交换机，routingKey不起作用，它向所有的队列发送广播，只要队列绑定到该交换机即接受消息。
		this.rabbitTemplate.convertAndSend(RabbitRoutingConstant.EXCHANGE, RabbitRoutingConstant.QUEUE_KEY_A,contentA);
		logger.info("Send ok,"+new Date()+","+contentA);
		this.rabbitTemplate.convertAndSend(RabbitRoutingConstant.EXCHANGE, RabbitRoutingConstant.QUEUE_KEY_B,contentB);
		logger.info("Send ok,"+new Date()+","+contentB);
	}
}